package audio

import (
	"bytes"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"io"
	"mime/multipart"
	"moko/internal/response"
	"net/http"
)

// HandleAppFileUpload 处理App端文件上传的处理器函数
func HandleAppFileUpload(c *gin.Context) response.Response {
	w := c.Writer
	r := c.Request

	// 从请求中获取上传的文件，这里假设文件是通过表单中的文件字段上传的，字段名为"file"
	file, header, err := r.FormFile("file")
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return response.CommonResponse[string]("获取上传文件失败", http.StatusBadRequest, err.Error())
	}
	defer file.Close()

	// 创建一个缓冲区来存储 multipart/form-data 请求体
	body := &bytes.Buffer{}

	// 创建 multipart writer
	writer := multipart.NewWriter(body)

	// 创建文件字段
	part, err := writer.CreateFormFile("file", header.Filename)
	if err != nil {
		return response.CommonResponse[string]("创建文件字段失败", http.StatusInternalServerError, err.Error())
	}

	// 将文件内容复制到 multipart writer
	_, err = io.Copy(part, file)
	if err != nil {
		return response.CommonResponse[string]("复制文件内容失败", http.StatusInternalServerError, err.Error())
	}

	// 关闭 writer 以完成 multipart/form-data 请求体的构建
	err = writer.Close()
	if err != nil {
		return response.CommonResponse[string]("关闭 multipart writer 失败", http.StatusInternalServerError, err.Error())
	}

	// 创建HTTP请求
	req, err := http.NewRequest("POST", "http://59.110.90.37:8058/audio/", body)
	if err != nil {
		return response.CommonResponse[string]("创建请求失败", http.StatusInternalServerError, err.Error())
	}

	// 设置请求头
	req.Header.Set("Content-Type", writer.FormDataContentType())

	// 发送请求
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return response.CommonResponse[string]("发送请求失败", http.StatusInternalServerError, err.Error())
	}
	defer resp.Body.Close()

	// 读取响应内容
	respBody, err := io.ReadAll(resp.Body)
	if err != nil {
		return response.CommonResponse[string]("读取响应失败", http.StatusInternalServerError, err.Error())
	}

	if resp.StatusCode != http.StatusOK {
		return response.CommonResponse[string]("上传文件失败", resp.StatusCode, string(respBody))
	}

	// 解码 JSON 响应到结构体
	var uploadResp response.AudioReport
	err = json.Unmarshal(respBody, &uploadResp)
	if err != nil {
		return response.CommonResponse[string]("解码 JSON 响应失败", http.StatusInternalServerError, err.Error())
	}

	w.WriteHeader(http.StatusOK)
	return response.CommonResponse("文件上传成功", http.StatusOK, uploadResp)
}

func HandleAppFileUploadVideo(c *gin.Context) response.Response {
	w := c.Writer
	r := c.Request

	// 从请求中获取上传的文件，这里假设文件是通过表单中的文件字段上传的，字段名为"file"
	file, header, err := r.FormFile("file")
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return response.CommonResponse[string]("获取上传文件失败", http.StatusBadRequest, err.Error())
	}
	defer file.Close()

	// 创建一个缓冲区来存储 multipart/form-data 请求体
	body := &bytes.Buffer{}

	// 创建 multipart writer
	writer := multipart.NewWriter(body)

	// 创建文件字段
	part, err := writer.CreateFormFile("file", header.Filename)
	if err != nil {
		return response.CommonResponse[string]("创建文件字段失败", http.StatusInternalServerError, err.Error())
	}

	// 将文件内容复制到 multipart writer
	_, err = io.Copy(part, file)
	if err != nil {
		return response.CommonResponse[string]("复制文件内容失败", http.StatusInternalServerError, err.Error())
	}

	// 关闭 writer 以完成 multipart/form-data 请求体的构建
	err = writer.Close()
	if err != nil {
		return response.CommonResponse[string]("关闭 multipart writer 失败", http.StatusInternalServerError, err.Error())
	}

	// 创建HTTP请求
	req, err := http.NewRequest("POST", "http://59.110.90.37:8056/upload/", body)
	if err != nil {
		return response.CommonResponse[string]("创建请求失败", http.StatusInternalServerError, err.Error())
	}

	// 设置请求头
	req.Header.Set("Content-Type", writer.FormDataContentType())

	// 发送请求
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return response.CommonResponse[string]("发送请求失败", http.StatusInternalServerError, err.Error())
	}
	defer resp.Body.Close()

	// 读取响应内容
	respBody, err := io.ReadAll(resp.Body)
	if err != nil {
		return response.CommonResponse[string]("读取响应失败", http.StatusInternalServerError, err.Error())
	}

	if resp.StatusCode != http.StatusOK {
		return response.CommonResponse[string]("上传文件失败", resp.StatusCode, string(respBody))
	}

	// 解码 JSON 响应到结构体
	var uploadResp response.VideoReport
	err = json.Unmarshal(respBody, &uploadResp)
	if err != nil {
		return response.CommonResponse[string]("解码 JSON 响应失败", http.StatusInternalServerError, err.Error())
	}

	w.WriteHeader(http.StatusOK)
	return response.CommonResponse("文件上传成功", http.StatusOK, uploadResp)
}
